package com.sinothk.hm.retrofit.demo.slice;

import com.sinothk.hm.retrofit.HiRetrofit;
import com.sinothk.hm.retrofit.bean.ResultData;
import com.sinothk.hm.retrofit.callback.DownloadCallback;
import com.sinothk.hm.retrofit.demo.ResourceTable;
import com.sinothk.hm.retrofit.demo.demo.AllApi;
import com.sinothk.hm.retrofit.demo.demo.BaseApi;
import com.sinothk.hm.retrofit.demo.demo.LoginVo;
import com.sinothk.hm.retrofit.param.RetrofitParam;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.usage.DataUsage;
import ohos.data.usage.Volume;
import ohos.global.resource.Resource;
import okhttp3.ResponseBody;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import retrofit2.Call;
import retrofit2.Response;
import rx.Subscriber;
import rx.schedulers.Schedulers;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.Executors;


public class MainAbilitySlice extends AbilitySlice {

    private Text helloTxt;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onActive() {
        super.onActive();

        helloTxt = (Text) findComponentById(ResourceTable.Id_text_helloworld);


        HiRetrofit.init(BaseApi.baseUrl)
                .create(AllApi.class)
                .login(new LoginVo("admin", "123456"))
                .subscribeOn(Schedulers.io())
//                .observeOn(HmSchedulers.mainThread())
                .subscribe(new Subscriber<ResultData<String>>() {
                    @Override
                    public void onCompleted() {
//                        Toast.makeText(MainActivity.this, "onCompleted", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e == null) {
                        }
//                        Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(ResultData<String> resultData) {
                        if (resultData != null) {
                        }
                        EventBus.getDefault().post(resultData);
                    }
                });
//        HiRetrofit.init(BaseApi.baseUrl, new HashMap<String,String>())
//                .create(AllApi.class)
//                .login(new LoginVo("admin", "123456"))
//                .subscribeOn(Schedulers.io())
////                .observeOn(HmSchedulers.mainThread())
//                .subscribe(new Subscriber<ResultData<String>>() {
//                    @Override
//                    public void onCompleted() {
////                        Toast.makeText(MainActivity.this, "onCompleted", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        if (e == null) {
//                        }
////                        Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onNext(ResultData<String> resultData) {
//                        if (resultData != null) {
//                        }
//                        EventBus.getDefault().post(resultData);
//                    }
//                });

//        File file = new File("/storage/emulated/0/Download/wKgANVvEPSeASGEFAAQ7wQP8jK4342.png");
//        HiRetrofit.init(BaseApi.baseUrl).create(AllApi.class)
//                .uploadFile("381518188", RetrofitParam.createFileParam("file", file))
//                .subscribeOn(Schedulers.io())
//                //.observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<ResultData<UserEntity>>() {
//                });

//        // 传单文件文件和实体
//        File file = new File("/storage/emulated/0/Download/wKgANVvEPSeASGEFAAQ7wQP8jK4342.png");
//        ArrayList<File> files = new ArrayList<>();
//        for (int i = 0; i < 3; i++) {
//            files.add(file);
//        }
//
//        HiRetrofit.init(BaseApi.baseUrl).create(AllApi.class)
//                .sendDaily("381518188", RetrofitParam.createFileListParam("file", files))
//                .subscribeOn(Schedulers.io())
//                //.observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<ResultData<UserEntity>>() {
//                });

        // 文件下载\
//        // http://app-global.pgyer.com/c72c378e0a5d827ebd94d2c880da01ec.apk?attname=mgdj-release_2.6.3_19_1112.apk&sign=c64d18d5db3ee659c5962e9e3a52c643&t=5bf2783d
//        String url = "dl/appdl/application/apk/f5/f5f27f9337934773bf2c18c807dac2dc/com.huawei.appmarket.2105251619.apk?maple=0&trackId=0&distOpEntity=HWSW";
//        String path = "/sdcard/21212.apk";
//        Optional<Volume> volume = DataUsage.getVolume(new File("/sdcard/example.txt"));
////        volume.ifPresent(theVolume -> {
////                    HiLog.info(LABEL, "isEmulated: %{public}t", theVolume.isEmulated());
////                    HiLog.info(LABEL, "isPluggable: %{public}t", theVolume.isPluggable());
////                    HiLog.info(LABEL, "Description: %{public}s", theVolume.getDescription());
////                    HiLog.info(LABEL, "Volume UUID: %{public}d", theVolume.getVolUuid());
////                }
////        );
//
//        HiRetrofit
//                .init("https://appdl-1-drcn.dbankcdn.com/", Executors.newSingleThreadExecutor())
//                .create(AllApi.class)
//                .download(url)
//                .enqueue(new DownloadCallback(getAbility(), path) {
//                    @Override
//                    public void onStart() {
//                        String txt = helloTxt.getText() + "\n" + "onStart";
//                        helloTxt.setText(txt);
//                    }
//
//                    @Override
//                    public void onProgress(long currSize, long totalSize, int progress) {
//                        String txt = helloTxt.getText() + "\n" + "progress = " + progress;
//                        helloTxt.setText(txt);
//                    }
//
//                    @Override
//                    public void onFinish(String path) {
//                        String txt = helloTxt.getText() + "\n" + "onFinish";
//                        helloTxt.setText(txt);
//                    }
//
//                    @Override
//                    public void onFail(String errorInfo) {
//                        String txt = helloTxt.getText() + "\n" + "onFail";
//                        helloTxt.setText(txt);
//                    }
//                });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBusCallback(ResultData<String> resultData) {

        new ToastDialog(this).setText(resultData.getMessage()).show();

        helloTxt.setText(resultData.getResult());
//        if (result.eventType != "getReplyByCode") {
//            return
//        }
//
//        when (result.code) {
//            NetResult.SUCCESS -> {
//                if (result.data != null) {
//                } else {
//                    XUtils.toast().show("查无结果")
//                }
//            }
//            else -> {
//                XUtils.toast().show(result.msg)
//            }
//        }
    }
}
